<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="50px">
      <el-form-item label="客户" prop="feeUserName">
        <el-input
          v-model="queryParams.feeUserName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.fee_prestore_type_options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.pay_order_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >预存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-back"
          size="mini"
          @click="handleRefund"
        >退费</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border  v-loading="loading" :data="prestoreList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="客户姓名" align="center" prop="feeUserName" />
      <el-table-column label="发生金额" align="center" prop="amt" />
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fee_method_options" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fee_prestore_type_options" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_order_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column label="操作时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="单号" align="center" prop="orderNo" width="180"/>
      <el-table-column label="操作" align="center" fiexd="right" width="240px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            v-show="scope.row.type=='pay'"
            :disabled="scope.row.state=='2'"
            @click="handleConfirmPay(scope.row)"
          >确定收费</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            v-show="scope.row.type=='refund'"
            :disabled="scope.row.state=='5'"
            @click="handleConfirmRefund(scope.row)"
          >确定退费</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="handlePrintAdd(scope.row)"
          >打印单据</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewOwner(scope.row)"
          >客户信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <prestore-add ref="prestoreAdd" @after="getList" v-if="openAdd"></prestore-add>
    <prestore-refund ref="prestoreRefund" @after="getList" v-if="openRefund"></prestore-refund>

    <user-detail ref="userDetail" v-if="showDetail"></user-detail>
  </div>
</template>

<script>
import { listPrestore ,confirmPay,confirmRefund} from "@/api/pay/prestore";
import prestoreAdd from "@/views/pay/prestore/prestoreAdd";
import prestoreRefund from "@/views/pay/prestore/prestoreRefund";
import userDetail from "@/views/owner/user/detail/index"
export default {
  name: "Prestore",
  components:{
    prestoreAdd,
    prestoreRefund,
    userDetail
  },
  dicts: ['pay_order_state', 'fee_method_options','fee_prestore_type_options'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 费用预存表格数据
      prestoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示预存
      openAdd: false,
      // 是否显示退费
      openRefund: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feeUserId: null,
        feeUserName: null,
        payType: null,
        type:null,
        state: null,
        orderNo: null,
      },
      //是否打开详情
      showDetail:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询费用预存列表 */
    getList() {
      this.loading = true;
      listPrestore(this.queryParams).then(response => {
        this.prestoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //打印收款单
    handlePrintAdd(row){

    },
    /** 新增预存操作 */
    handleAdd() {
      this.openAdd = true;
      this.$nextTick(() => {
        this.$refs.prestoreAdd.init();
      });
    },
    /** 确认收款操作 */
    handleConfirmPay(row){
      this.$modal.confirm(`是否确认【${row.feeUserName}】收费？`).then(function() {
        return confirmPay(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("确认成功");
      }).catch(() => {});
    },
    /** 新增退费操作 */
    handleRefund() {
      this.openRefund = true;
      this.$nextTick(() => {
        this.$refs.prestoreRefund.init();
      });
    },
    /** 确认退款操作 */
    handleConfirmRefund(row){
      this.$modal.confirm(`是否确认【${row.feeUserName}】退费？`).then(function() {
        return confirmRefund(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("确认成功");
      }).catch(() => {});
    },
    //客户信息
    handleViewOwner(row){
      this.showDetail = true;
      this.$nextTick(() => {
        this.$refs.userDetail.init(row.feeUserId);
      });
    }
  }
};
</script>
